#ifndef __CMDCHANNEL_H__
#define __CMDCHANNEL_H__

#include "SocketTcpClient.h"
#include "MyThread.h"
#include "WaiterList.h"
#include "ListPtr.h"
#include "Lock.h"
#include "define.h"


class CManager;
class CCmdChannel : public CMyThread
	,IOTcpClientNotify
{
public:
	CCmdChannel(CManager *pMan);
	~CCmdChannel(void);

	bool Start(void);
	void Stop(void);

	bool GetValid(void);
	int SendUserVerify(const char *pUser, const char *pPassWord, char *outMsg, int *msgLen);

	bool GetUserInfo(char *outMsg, int *len);
	bool GetUserList(char *outMsg, int *len);

	bool GetMeetingFileList(char *outMsg, int *len);
	bool GetMeetingIssueList(char *outMsg, int *len);	

	bool GetMeetingVoteList(char *outMsg, int *len);
	bool TerminalUserSign(int nUserId, const char *pUserName, const char *pRemotePath);

	bool UserSignControl(int nControlType);
	bool AllUserSign(const char *pMsgJson);

	bool OpenScreenBroadcast();
	bool CloseScreenBroadcast(int nUserId);

	bool ReplyScreenBroadcastReq(int nUserId, int nResult);

	int InviteScreenBroadcast(int nUserId, int nType, bool bForce);
	int ScreenBroadcastControl(int nType);

	int UploadFileInfo(TerminalFileInfo *pTerminalFile);
	bool SendChatMessage(const char *pChatMessage);

	int StartWbRequest(int nUserId, char *outMsg, int *len);
	int CloseWb(int nUserId);

	int IssueControl(int nIssueId, int nControlType);
	int VoteControl(int nVoteId, int nControlType);

	int VoteMsg(const char *pMsg, int nLen);
	int CenterControl(int nControlType, const char *pTerminalId);

	bool SendMeetingService(const char *pMessage);
	bool InviteTerminalSpeak(int nTerminalId);

	bool TerminalApplySpeak(void);
	bool ApplaySpeakRsp(int nTerminalId, int nResult, const string &strError);

	bool InviteSpeakRsp(const string &strJson);
	bool StopSpeak(int nTerminalId);

	bool ReportSpeakIsOK(int nTerminalId, int nSpeakId, int nStatus);

	bool RequestDesktop(int nTerminalId);
	bool RequestDesktopRsp(int nTerminalId, int nResult);

	bool PushDesktop(int nTerminalId);
	bool PushDesktopRsp(int nTerminalId, int nResult);

	bool PushDesktopToScreen(int nTerminalId, const char *pScreen, int nType);
	bool CloseStream(int nInTerminalId, int nOutTerminalId);

	bool PushDesktopToScreenRsp(const char *pScreen, int nResult);

protected:
	virtual void ThreadProcMain(void);
	virtual void OnRecvTcpServerDataNotify(const int nCmd, const int nPacketNum, const int nSendId, const char *szBuf, int nLen);
	virtual void OnDisConnTcpServerNotify(const int nType);

private:
	int ConnectServer(void);
	void HandleRecvData(RecvDataInfo *pRecvData);

	int SendData(int nCmd, const char *pData, int nDataLen);
	bool SendDataEx(int nCmd, const char *pData, int nDataLen, unsigned long &param);
	
	void HandleRspMsg(RecvDataInfo *pRecvData);
	void HandleChannelDeclareRsp(RecvDataInfo *pRecvData);

	bool SendChannelDeclare(void);
	bool SendHeartbetMsg(void);

	void ClearRecvData(RecvDataInfo *pRecvData);

private:
	CSocketTcpClient	*m_pClient;
	CManager			*m_pMan;

	CWaiterList			*m_pWaitList;

	bool				m_bExit;
	bool				m_bConnect;

	bool				m_bValid;
	bool				m_bFirstConnect;

	unsigned long		m_ulLastSendTime;
	unsigned long		m_ulLastConnectTime;

	unsigned short		m_usPacketNum;

	ListPtr				m_lstRecvData;
	CLock				m_lockRecvData;

};
#endif
